﻿layui.use(['table', 'dropdown', 'layer', 'form'], function () {

    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    $(function () {
        Fan();

    });
    //绑定下拉框
    $.ajax({
        url: "../Quality/DeviceDo",
        type: "get",
        data: { token: localStorage.getItem("token") },
        success: function (data) {
            var obj = JSON.parse(data);
            var options = '<option value="">请选择</option>'; // 添加默认选项
            $.each(obj.data.list, function (index, item) {
                console.log(item)
                options += '<option value="' + item.qualitygroupId + '">' + item.qualitygroupName + '</option>';
            });
            $("#QualitygroupId").html(options);
            form.render('select'); // 重新渲染下拉框
        },
        error: function () {
            console.log("获取数据失败");
        }
    });
    window.Fan = function Fan() {
       var qualityPlanCode = localStorage.getItem("qualityPlanCode")
        console.log(qualityPlanCode)

        $.ajax({
            url: '../Quality/QpianBackfill',
            data: {
                QualityPlanCode: qualityPlanCode,
                token: ""
            },
            type: 'Get',
            success: function (res) {
                var res = JSON.parse(res)
      console.log(res)
                localStorage.setItem("qualityprogramId", res.data.qualityprogramId);
                localStorage.setItem("qualityPlanId", res.data.qualityPlanId);
                console.log(localStorage.getItem("qualityprogramId"))
               
                $("#QualityPlanCode").val(res.data.qualityPlanCode)
                
                $("#QualityPlanName").val(res.data.qualityPlanName)
                $("#QualityPlanSart").val(res.data.qualityPlanSart)
                
                $('#QualityPlanSart').text(res.data.qualityPlanSart == 1 ? "启用" : "禁用")
                $("#QualitygroupId").val(res.data.qualitygroupId)
      
                form.render('select'); // 重新渲染checkbox表单元素
                $.each(res, function () {
                    console.log(res.data.qualityinspection)
                
                    // 先进行表格的初始渲染
                    table.render({
                        elem: '#carriertable',  // 表格元素的选择器
                        page: {
                            limit: 5,
                            limits: [5, 8, 10]
                        },
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'qualityinspectionName', title: '质检项目名称' },
                            { field: 'qualityinspectionBH', title: '质检项目编号' },
                            { field: 'qualityinspectionmethood', title: '质检方法' },
                            { field: 'qualityinspectiontool', title: '质检工具' },
                            { field: 'qualityinspectionmetype', title: '质检类型' },
                            { field: 'standardvalue', title: '标准值' },
                            { field: 0, title: '上差值' },
                            { field: 0, title: '下差值' },
                            {
                                field: '', width: 260, title: '操作', templet: function (d) {
                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="FanUpdate(' + d.qualityinspectionId + ')"><i class="layui-icon layui-icon-edit"></i>质检记录</a> ';
                                      ;
                                }

                            }
                        ]],
                        // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                        data: res.data.qualityinspection
                    });




                })
            }




        })

    }
    //质检修改

    window.QyalityUpdate = function QyalityUpdate() {
      
         QualityPlanCode = $("#QualityPlanCode").val()
        QualityPlanName = $("#QualityPlanName").val()
        QualityPlanSart = $("[name='QualityPlanSart']:checked").val()
        QualitygroupId = $("#QualitygroupId").val()
        console.log(QualityPlanCode)
        console.log(QualityPlanName)
        console.log(QualityPlanSart)
        console.log(QualitygroupId)
        // 取质检项目id
        var checkStatus = table.checkStatus('carriertable');
        var selectedIds = checkStatus.data.map(function (item) { return item.qualityinspectionId; });
        console.log(selectedIds)
        $.ajax({
            url: '../Quality/QualityspectionUodate',
            type: 'post',
            data: {
                QualityPlanId: localStorage.getItem("qualityPlanId"),
                QualityPlanCode: QualityPlanCode,
                QualityPlanName: QualityPlanName,
                QualityPlanSart: QualityPlanSart,
                QualitygroupId: QualitygroupId,
                QualityinspectionId: selectedIds,
                token:""
            },
            //contentType: 'application/json',
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                if (res.data > 0) {
                    alert("修改成功")
                    window.location.href = '/Quality/QgroupPinShow'
                }
                else {
                    alert("修改失败")
                }

            }
        })

   }




})